package com.fenghuajueli.module_user.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.fenghuajueli.libbasecoreui.ui.PrivacyWebActivity;
import com.fenghuajueli.libbasecoreui.constants.PrivacyConstantsUtils;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.databinding.DialogPrivacyTipBinding;

public class PrivacyTipDialog extends Dialog {

    private OnButtonClickListener onButtonClickListener;


    public PrivacyTipDialog(Context context) {
        super(context, com.fenghuajueli.libbasecoreui.R.style.StyleBaseDialog);
    }

    private DialogPrivacyTipBinding binding;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogPrivacyTipBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) binding.llContainer.getLayoutParams();
        int start = SizeUtils.dp2px(32);
        layoutParams.setMarginStart(start);
        layoutParams.setMarginEnd(start);
        binding.llContainer.setLayoutParams(layoutParams);


        initPrivacy();

        binding.btnCancel.setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onLeftButtonClick();
            }
            dismiss();
        });
        binding.btnConfirm.setOnClickListener(v -> {
            if (onButtonClickListener != null) {
                onButtonClickListener.onRightButtonClick();
            }
            dismiss();
        });
    }


    private void initPrivacy() {
        String first = "为了更好的保障您的合法权益，请您认真阅读一下协议";
        String second = "《用户协议》";
        String third = "《隐私政策》";
        SpannableString spannableString = new SpannableString(first + second + third);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(getContext(), R.color.priviceTextColor));
                //超链接形式的下划线，false 表示不显示下划线，true表示显示下划线,其实默认也是true，如果要下划线的话可以不设置
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
                PrivacyWebActivity.start(getContext(), PrivacyConstantsUtils.ProtocolType.USER_PROTOCOL);
            }
        };
        spannableString.setSpan(clickableSpan, first.length(), first.length() + second.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(getContext(), R.color.priviceTextColor));
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
                PrivacyWebActivity.start(getContext(), PrivacyConstantsUtils.ProtocolType.PRIVACY);
            }
        };
        spannableString.setSpan(clickableSpan, first.length() + second.length(), spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.tvPrivacy.setText(spannableString);
        binding.tvPrivacy.setHighlightColor(Color.TRANSPARENT);
        binding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance());
    }


    public interface OnButtonClickListener {

        void onLeftButtonClick();

        void onRightButtonClick();
    }

}
